package com.able.mmbs;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by xanadw on 10/24/2021.
 */
public class LeDeviceListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> deviceArrayList;
    private Context context;
    private LayoutInflater mInflater;
    private List<Integer> mRssis;
    private List<String> mBletype;


    public LeDeviceListAdapter(Context context, ArrayList<BluetoothDevice> devices, List<Integer> rssis, List<String> bletype) {
        this.context = context;
        this.deviceArrayList = devices;
        mInflater = LayoutInflater.from(this.context);
        mRssis = rssis;
        mBletype = bletype;
    }

    @Override
    public int getCount() {
        return deviceArrayList.size();
    }

    @Override
    public BluetoothDevice getItem(int position){
        return deviceArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder mViewHolder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.ble_listview, parent, false);
            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);
        }else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        BluetoothDevice currentListData = getItem(position);

        if (currentListData.getName()==null) {
            mViewHolder.tvname.setText("No name");
        }
        else{
            mViewHolder.tvname.setText(currentListData.getName());
        }
        mViewHolder.tvmac.setText(currentListData.getAddress());
        mViewHolder.tvRssi.setText(String.format("%ddBm", mRssis.get(position)));
        mViewHolder.tvBletype.setText(mBletype.get(position));

        return convertView;
    }

    private class MyViewHolder {
        TextView tvname, tvmac, tvRssi, tvSate, tvBletype;

        public MyViewHolder(View item) {
            tvname = (TextView)item.findViewById(R.id.name);
            tvmac = (TextView)item.findViewById(R.id.mac);
            tvRssi = (TextView)item.findViewById(R.id.rssi);
            tvBletype = (TextView)item.findViewById(R.id.bluetoothtype);
        }
    }

    public void clear() {
        deviceArrayList.clear();
        mRssis.clear();
        mBletype.clear();
    }

    public boolean addDevice (BluetoothDevice device) {
        boolean returnValue;
        if (!deviceArrayList.contains(device)) {
            deviceArrayList.add(device);
            returnValue = true;
        } else {
            returnValue = false;
        }

        return returnValue;
    }

    public void addBound() {
        Set<BluetoothDevice> bondedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
        if (bondedDevices != null)
            deviceArrayList.addAll(bondedDevices);
    }
}